/*
 * Trilogy instant messaging system
 * Nathan Reitz, Aaron Mertz, Dave Clark
 * 11/14/2007
*/
package edu.psu.ist.ist311.trilogyIM;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Conversation implements ActionListener{
	Sender sender;
	Receiver receiver;
	Message message;
	ChatWindow chatwindow;
	
	public Conversation(Sender sender, Receiver receiver){
		//sets the sender(user) and the receiver for the conversation
		this.sender = sender;
		this.receiver = receiver;
		
		//registers the conversation with each party
		this.sender.attachConversation(this);
		this.receiver.attachConversation(this);
		
		//loads the chat window
		chatwindow = new ChatWindow(receiver.getReceiverName());
		
		chatwindow.pack(); //size a Window to fit the preferred size and layouts of its subcomponents. 
		chatwindow.setVisible(true);	//makes window visible
		
		//starts listening for sending messages
		chatwindow.sendText.addActionListener(this);
		chatwindow.confirm.addActionListener(this);
	}

	
	//public String getReceiverName(){}
	//public String getSenderName(){}	

	
	
	//sets the sender of the convo
	public void setSender(Sender sender)
	{
		this.sender = sender;
	}
	
	//sets the reciever of the convo
	public void setReceiver(Receiver receiver)
	{
		this.receiver = receiver;
	}
	
	//handles actions stemming from chatwindow
	public void actionPerformed(ActionEvent evt) {
		Object event = evt.getSource();
		if((event==chatwindow.sendText)||(event==chatwindow.confirm)){	//when return is pressed or button is clicked
			sender.sendMessage(chatwindow.getMessageText());
		}
	}
	
	//sends the message to the chat window
	public void addMessage(Message msg){
		chatwindow.appendText(msg.returnMessage());
	}
	
}
